﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExcelToOracle.XmlManagement
{
    public partial class CreateXmlForm : Form
    {
        private DataTable _constantTable;
        private DataTable _fieldTable;

        public CreateXmlForm()
        {
            InitializeComponent();
        }

        private void _ctlMenuItemExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void _ctlMenuItemSave_Click(object sender, EventArgs e)
        {
            if (_ctlGridConstant.DataSource == null
                || _ctlGridField.DataSource == null
                || _ctlGridData.DataSource == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(_ctlEditTableName.Text))
            {
                MessageBox.Show("输入表名!","提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + "Configs";
            dlg.Filter = "配置文件(*.xml)|*.xml";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                List<FieldTplt> consts = new List<FieldTplt>();
                foreach (DataGridViewRow row in _ctlGridConstant.Rows)
                {
                    if (row.Cells[0].Value != null)
                    {
                        string db = Convert.ToString(row.Cells[0].Value);
                        string type = Convert.ToString(row.Cells[1].Value);
                        if (!string.IsNullOrEmpty(db))
                        {
                            FieldTplt constField = new FieldTplt(
                                string.Empty,
                                db,
                                type);
                            consts.Add(constField);
                        }
                    }
                }

                List<FieldTplt> fields = new List<FieldTplt>();
                foreach (DataGridViewRow row in _ctlGridField.Rows)
                {
                    string excel = Convert.ToString(row.Cells[0].Value);
                    string db = Convert.ToString(row.Cells[1].Value);
                    string type = Convert.ToString(row.Cells[2].Value);
                    if (!string.IsNullOrEmpty(excel) && !string.IsNullOrEmpty(db))
                    {
                        FieldTplt field = new FieldTplt(
                            excel,
                            db,
                            type);
                        fields.Add(field);
                    }
                }

                ImportTemplate template = ImportTemplate.Build(
                    _ctlEditTableName.Text,
                    consts,
                    fields);

                if (ImportTemplate.Save(dlg.FileName, template))
                {
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("保存失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void OnMenuItemOpenClick(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "excel文件(.xlsx)|*.xlsx";
            dlg.Multiselect = false;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ExcelUtil util = new ExcelUtil(dlg.FileName);
                try
                {
                    SelectSheetDialog sheetDlg = new SelectSheetDialog(util.GetSheets());
                    if (sheetDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        // 获取前20行用于预览
                        DataTable table = util.GetData(sheetDlg.SelectedSheet, 20);
                        ParseData(table);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    util.Close();
                }
            }
        }

        private void ParseData(DataTable table)
        {
            _constantTable = new DataTable();
            _constantTable.Columns.Add("Db", typeof(string));
            _constantTable.Columns.Add("Type", typeof(string));

            _fieldTable = new DataTable();
            _fieldTable.Columns.Add("Excel", typeof(string));
            _fieldTable.Columns.Add("Db", typeof(string));
            _fieldTable.Columns.Add("Type", typeof(string));

            for (int index = 0; index< table.Columns.Count;index++)
            {
                DataColumn column = table.Columns[index];
                column.ColumnName = AA24.ToAA(AA24.FromF27(column.ColumnName));

                DataRow newRow = _fieldTable.NewRow();
                newRow.ItemArray = new object[] {
                    column.ColumnName,
                    string.Empty,
                    string.Empty
                };

                _fieldTable.Rows.Add(newRow);
            }

            _ctlGridConstant.DataSource = _constantTable;
            _ctlGridField.DataSource = _fieldTable;
            _ctlGridData.DataSource = table;

            foreach (DataGridViewRow row in _ctlGridField.Rows)
            {
                string name = row.Cells[0].Value.ToString();

                foreach (DataGridViewColumn column in _ctlGridData.Columns)
                {
                    column.SortMode = DataGridViewColumnSortMode.NotSortable;
                    if (name == column.Name)
                    {
                        row.Tag = column;
                        break;
                    }
                }
            }
        }
    }
}
